package com.freetymekiyan.algorithms.level.easy;

import java.util.ArrayList;
import java.util.List;

/**
 * Given numRows, generate the first numRows of Pascal's triangle.
 * <p>
 * For example, given numRows = 5,
 * Return
 * <p>
 * [
 * [1],
 * [1,1],
 * [1,2,1],
 * [1,3,3,1],
 * [1,4,6,4,1]
 * ]
 * <p>
 * Tags: Array
 */
class PascalsTriangle {
  public static void main(String[] args) {

  }

  /**
   * Definition
   */
  public List<List<Integer>> generate(int numRows) {
    List<List<Integer>> triangle = new ArrayList<List<Integer>>();
    if (numRows <= 0) return triangle;

    List<Integer> firstRow = new ArrayList<Integer>();
    firstRow.add(1);
    triangle.add(firstRow);

    for (int i = 1; i < numRows; i++) {
      List<Integer> lastRow = triangle.get(i - 1);
      List<Integer> row = new ArrayList<Integer>(i + 1);

      for (int j = 0; j < i + 1; j++) {
        if (j == 0 || j == i) {
          row.add(1);
        } else {
          row.add(lastRow.get(j - 1) + lastRow.get(j));
        }
      }
      triangle.add(row);
    }
    return triangle;
  }
}